﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Film.Repository;
using AjaxControlToolkit;

namespace PhuongDoAn.UC
{
    public partial class uc_rating : System.Web.UI.UserControl
    {
        tblMovieRepository _movieService = new tblMovieRepository();
        tblRatingRepository _ratingService = new tblRatingRepository();
        int pageSize = 2;
        int pageSegSize = 1;
        public int count = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void rMyRate_Changed(object sender, AjaxControlToolkit.RatingEventArgs e)
        {
            AjaxControlToolkit.Rating rMyRate = (AjaxControlToolkit.Rating)sender;
           // Label rvid = ((Control)sender).Parent.FindControl("rvid") as Label;
          //  Response.Write(rvid.Text + rMyRate.CurrentRating);
            Response.Write(e.Value.ToString() + " : " + rMyRate.CurrentRating);

            e.CallbackResult = "";
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {

                try
                {

                    Page.ClientScript.GetPostBackEventReference(this, string.Empty);

                    int currentPage = 1;

                    string eventTarget = this.Request["__EVENTTARGET"];
                    string eventArgument = this.Request["__EVENTARGUMENT"];
                    if (eventTarget != String.Empty && eventTarget == "pageIndex")
                    {
                        currentPage = Convert.ToInt32(eventArgument);
                    }

                    //Response.Write(eventArgument);
                    int start = pageSize * (currentPage - 1);

                    List<tblMovie> result = new List<tblMovie>();

                    result = _movieService.Filter("", start, pageSize, out count);

                    rptRating.DataSource = result;
                    rptRating.DataBind();

                    if (count <= pageSize * 1)
                    {
                        cbPaging.Visible = false;
                    }
                    else
                    {
                        cbPaging.Items.Clear();
                        int pageCount = (int)Math.Ceiling((double)count / pageSize);
                        lblTotalPage.Text = string.Format("{0} trang", pageCount);
                        for (int i = 1; i <= pageCount; i++)
                        {
                            cbPaging.Items.Add(i.ToString());
                        }
                        cbPaging.SelectedIndex = currentPage - 1;
                    }

                }
                catch (Exception ex)
                {
                    Page.Response.Write("error" + " : " + ex.Message + " : " + ex.StackTrace);
                }
            }
        }

        void LoadList()
        { 
            int count = 0;
            rptRating.DataSource = _movieService.Filter("", 1, 5, out count, true);
            rptRating.DataBind();
        }

        protected void btnCapNhat_Click(object sender, EventArgs e)
        {
            string appent = "";
            List<tblRating> rating = new List<tblRating>();
            foreach (RepeaterItem item in rptRating.Items)
            {
                Rating rf = (Rating)item.FindControl("rMyRate");
                DropDownList dr = (DropDownList)item.FindControl("drState");
                HiddenField hf = (HiddenField)item.FindControl("hfID");
                if (hf != null && rf != null && dr != null)
                {
                    tblRating rate = new tblRating();
                    rate.MovieID = Convert.ToInt32(hf.Value);
                    rate.Rating = rf.CurrentRating;
                    rating.Add(rate);
                    //rf.CurrentRating
                    //appent = string.Format("{0}-{1}", appent, dr.Text);
                    //appent = string.Format("{0}-{1}", appent, rf.CurrentRating);
                }
            }
       //     _ratingService.AddList(rating);
            //Response.Write(rMyRate1.CurrentRating);
            //lblThongBao.Text = appent;
        }        

    }
}